﻿Imports Oracle.DataAccess.Client
Imports lbGlobalcom
Public Class fListaRecibos

    Private lDocumento As String
    Private lNumero As Integer
    Public ReadOnly Property pDocumento() As String
        Get
            Return lDocumento
        End Get
    End Property

    Public ReadOnly Property pNumero() As Integer
        Get
            Return lNumero
        End Get
    End Property


    Private Sub btnBuscaTercero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Dim fBT As New fBuscaTercero
            fBT.TipoTercero = "Cliente"
            If fBT.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtTercero.Text = fBT.midTercero
                txtSucursal.Text = fBT.mSucursal
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub fListaRecibos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dtpFechaInicial.Value = dtpFechaFinal.Value.AddDays(-30)
    End Sub

    Private Sub btnBuscaTercero_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTercero.Click
        Try
            Dim fBT As New fBuscaTercero
            fBT.TipoTercero = "Cliente"
            If fBT.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtTercero.Text = fBT.midTercero
                txtSucursal.Text = fBT.mSucursal
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        cmd.CommandText = "SELECT * FROM CAR_RECIBOSCAJA "
        Dim qWhere As String = "WHERE "
        Dim CuentaW As Int16 = 0
        If Me.dtpFechaInicial.Text.Trim.Length > 0 Then
            CuentaW += 1
            qWhere &= "FECHA >= '" & FormatDateTime(Me.dtpFechaInicial.Value.Date, DateFormat.ShortDate) & "'"
        End If
        If Me.dtpFechaFinal.Text.Trim.Length > 0 Then
            If CuentaW > 0 Then _
               qWhere &= " AND "
            CuentaW += 1
            'qWhere &= "FECHA <= TO_DATE('26012009','DDMMYYYY')"
            qWhere &= "FECHA <= '" & FormatDateTime(Me.dtpFechaFinal.Value.Date, DateFormat.ShortDate) & "'"
        End If
        If Me.txtTercero.Text.Trim.Length > 0 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "IDTERCERO =" & Me.txtTercero.Text & ""
        End If
        If Me.txtSucursal.Text.Trim.Length > 0 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "SUCURSAL ='" & Me.txtSucursal.Text & "'"
        End If
        If CuentaW = 0 Then _
            qWhere = ""
        cmd.CommandText &= qWhere
        Try
            Conn.Open()
            DsCartera.CAR_RECIBOSCAJA.Rows.Clear()
            DsCartera.CAR_RECIBOSCAJA.Load(cmd.ExecuteReader)
            Conn.Close()
            CAR_RECIBOSCAJADataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub


    Private Sub AsignaRecibo(ByVal Fila As Integer)
        If Fila >= 0 Then
            lDocumento = CAR_RECIBOSCAJADataGridView.Rows(Fila).Cells("IDDOCUMENTO").Value
            lNumero = CAR_RECIBOSCAJADataGridView.Rows(Fila).Cells("NUMERO").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub CAR_RECIBOSCAJADataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles CAR_RECIBOSCAJADataGridView.CellMouseDoubleClick
        AsignaRecibo(e.RowIndex)
    End Sub


    Private Sub CAR_RECIBOSCAJADataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CAR_RECIBOSCAJADataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaRecibo(CAR_RECIBOSCAJADataGridView.CurrentRow.Index)
        End If
    End Sub
End Class